package com.dqhl.business.Base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.dqhl.business.MyApplication;
import com.dqhl.business.modle.User;
import com.dqhl.business.util.UserSaveUtil;
import com.dqhl.business.view.CustomProgressDialog;

/**
 * Created by mac on 16/11/2.
 * BaseActivity
 */
public class BaseActivity extends AppCompatActivity {

    private Toast mToast;
    protected MyApplication app;
    private CustomProgressDialog mCustomProgressDialog;
    protected User user;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApplication) getApplicationContext();
        app.addActivity(this);
        user = (User) UserSaveUtil.readUser(this);
    }

    /**
     * 直接跳转到另一个activity
     *
     * @param classObj 要跳转的activity
     */
    public void overlay(Class<?> classObj) {
        Intent intent = new Intent(this, classObj);
        startActivity(intent);
    }

    /**
     * 带有Bundle参数的跳转到另一个activity
     *
     * @param classObj 要跳转的activity
     * @param bundle   要传递的Bundle
     */
    public void overlay(Class<?> classObj, Bundle bundle) {
        Intent intent = new Intent(this, classObj);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 以startActivityForResult方式启动另一个activity
     *
     * @param classObj 要跳转的activity
     */
    public void overlay(Class<?> classObj, int requestCode) {
        Intent intent = new Intent(this, classObj);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 带有Bundle参数的以startActivityForResult方式启动另一个activity
     *
     * @param classObj 要跳转的activity
     * @param bundle   要传递的Bundle
     */
    public void overlay(Class<?> classObj, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, classObj);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动另一个Activity，并结束当前Activity
     *
     * @param classObj 要跳转的activity
     */
    public void forward(Class<?> classObj) {
        Intent intent = new Intent(this, classObj);
        startActivity(intent);
        finish();
    }

    /**
     * 带有Bundle参数的启动另一个Activity，并结束当前Activity
     *
     * @param classObj 要跳转的activity
     * @param bundle   要传递的Bundle
     */
    public void forward(Class<?> classObj, Bundle bundle) {
        Intent intent = new Intent(this, classObj);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }

    /**
     * Toast
     *
     * @param resId 资源信息
     */
    public void toast(int resId) {
        String msg = getResources().getString(resId);
        mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * Toast
     *
     * @param msg 文字信息
     */
    public void toast(String msg) {
        mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * 显示自定义ProgressBar，提示用户操作正在处理中
     *
     * @param content:要显示的提示内容包括ProgressBar和text
     */
    public void showCustomLoadBar(String content) {
        if (mCustomProgressDialog == null) {
            mCustomProgressDialog = new CustomProgressDialog(this, content);
            mCustomProgressDialog.setCanceledOnTouchOutside(false);
        } else {
            mCustomProgressDialog.setTitle(content);
        }
        mCustomProgressDialog.show();
    }

    /**
     * 显示自定义ProgressBar，提示用户操作正在处理中
     *
     * @param resId
     */
    public void showCustomLoadBar(int resId) {
        String content = getResources().getString(resId);
        if (mCustomProgressDialog == null) {
            mCustomProgressDialog = new CustomProgressDialog(this, content);
            mCustomProgressDialog.setCanceledOnTouchOutside(false);
        } else {
            mCustomProgressDialog.setTitle(content);
        }
        mCustomProgressDialog.show();
    }

    /**
     * 释放自定义ProgressBar，提示操作结果
     */
    public void hideCustomLoadBar() {
        if (mCustomProgressDialog != null && mCustomProgressDialog.isShowing()) {
            mCustomProgressDialog.cancel();
        }
    }

    /**
     * 点击其他地方时，将软键盘隐藏
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        app.finishActivity(this);
    }
}
